package com.ijiuchuang.jc_localization.tools.utils;

import android.graphics.Color;
import android.support.annotation.ColorInt;

/**
 * Created by 王伟 on 2016/7/21
 * Function : named from material design color
 */
public class MColor extends Color {

    @ColorInt public static final int PrimaryWhite       = 0xddFFFFFF;
    @ColorInt public static final int SecondaryWhite       = 0x89FFFFFF;
    @ColorInt public static final int PrimaryBlack       = 0xdd000000;
    @ColorInt public static final int SecondaryBlack       = 0x89000000;

    /**
     * 色域枚举
     */
    public enum ColourGamut {
        RED, PINK, PURPLE, DEEP_PURPLE, INDIGO,
        BLUE, LIGHT_BLUE, CYAN, TEAL,
        GREEN, LIGHT_GREEN, LIME, YELLOW,
        AMBER, ORANGE, DEEP_ORANGE, BROWN,
        GREY, BLUE_GREY
    }

    /**
     * 获取随机颜色
     * @return 随机色
     */
    public static int getRandom() {
        int red = (int) (Math.random() * (255 + 1));
        int green = (int) (Math.random() * (255 + 1));
        int blue = (int) (Math.random() * (255 + 1));
        return MColor.argb(255, red, green, blue);
    }

    /**
     * 获取某色域的随机色
     * @param colourGamut 色域
     * @return 随机色，来源于 material design color
     */
    public static int getRandom(ColourGamut colourGamut) {
        return 0;
    }

    /**
     * 获取颜色亮度
     * @param color 颜色值
     * @return 亮度 0.0 ~ 1.0
     */
    public static double lightness(@ColorInt int color) {
        int r = (color >> 16) & 0xFF;
        int g = (color >> 8) & 0xFF;
        int b = color & 0xFF;

        int max = Math.max(b, Math.max(r, g));
        int min = Math.min(b, Math.min(r, g));
        return (max + min) / 2 / 255;
    }
    /**
     * 根据颜色返回对比度
     * @param color 颜色值
     * @return 对比度 0.0f ~ 1.0f
     */
    public static double contrast(@ColorInt int color) {
        int[] rgbaArray = new int[]{MColor.red(color), MColor.green(color), MColor.blue(color)};
        return 1 - ((0.00299 * (double) rgbaArray[0]) + (0.00587 * (double) rgbaArray[1]) + (0.00114 * (double) rgbaArray[2]));
    }
    /**
     * 根据背景颜色得到一个对比度，返回一个前景色
     * @param color 背景色
     * @return 前景色：黑 or 白
     */
    public static int getPrimaryTextColor(@ColorInt int color) {
        return contrast(color) < 0.5 ? MColor.PrimaryBlack : MColor.PrimaryWhite;
    }

    /**
     * 根据背景颜色得到一个对比度，返回一个二级前景色
     * @param color 背景色
     * @return 二级前景色：82.7%黑 or 82.7%白
     */
    public static int getSecondaryTextColor(@ColorInt int color) {
        return contrast(color) < 0.5 ? MColor.SecondaryBlack : MColor.SecondaryWhite;
    }
}
